// pages/company/companyCreate/companyCreate.js
const app = getApp()
var that;
var util = require('../../../utils/util.js');

Page({

  data: {
    industryArr: ["互联网", "建筑", "医疗", "教育", "金融", "其他"],
    industryIndex:-1,
    companyObj: {
      "closing": 0,
      "code": "",
      "createTime": "",
      "id": '',
      "logo": "",
      "ma": "",
      "name": "",
      "status": 0,
      "trade": "",
      "updateTime": ""
    },
    loadingType: false, //报名防止连续点击
    userId:""
  },

  onLoad: function (options) {
    that = this;

    wx.getStorage({
      key: 'loginUserObj',
      success: function (res) {
        that.setData({
          userId: res.data.id
        })
      }
    })
    
  },

  //公司名称
  inpNameFun: function (e) {
    that.data.companyObj.name = e.detail.value;
    that.setData({
      companyObj: that.data.companyObj
    })
  },

  // 所在行业
  chooseIndustryFun:function(e){
    console.log("e",e)
    that.data.companyObj.trade = that.data.industryArr[e.detail.value];
    that.setData({
      industryIndex: e.detail.value,
      companyObj: that.data.companyObj
    })
  },

  // 点击上传logo
  upLogoFun:function(){
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        console.log("chooseImage", res)

        util.upFileFun(res.tempFilePaths[0]).then(
          function (res) {
            console.log('upLoadFun suc', res)
            if (res.statusCode == 200) {
              var datas = JSON.parse(res.data);
              that.data.companyObj.logo = datas.data;
              that.setData({
                companyObj: that.data.companyObj
              })
            }
          }
        ).catch(
          function (err) {
            console.log('upLoadFun fail', err)
          }
        );

      }
    })
  },

  // 创建按钮点击 先判断信息填写是否全面
  saveBtnFun:function(){
    if (that.data.companyObj.name == ""){
      wx.showToast({
        title: '请填写名称',
        icon: 'none'
      })
    } else if (that.data.companyObj.trade == "") {
      wx.showToast({
        title: '请选择所在行业',
        icon: 'none'
      })
    } else if (that.data.companyObj.logo == "") {
      wx.showToast({
        title: '请上传logo',
        icon: 'none'
      })
    }else{
      
      if (that.data.loadingType == false ){
        that.setData({
          loadingType:true
        })
        that.saveBtnRegFun();
      }
    }
  },

  // 创建接口
  saveBtnRegFun:function(){

    wx.request({
      url: app.http + 'hysCompany/addCompany?userId=' + that.data.userId + '&page=' + 'pages/meetingDetails/meetingDetails',
      data: that.data.companyObj,
      method: 'POST',
      header: {
        'content-type': 'application/json', // 默认值
        'APP-User-Token': app.globalData.tokenMap.access_token
      },
      success: function (res) {
        console.log("底部确认suc返回", res)
        if (res.data.code == 200) {
          app.globalData.companyObj = res.data.data;
          wx.setStorage({
            key: 'companyObj',
            data: res.data.data,
          })
          wx.showToast({
            title: '创建成功',
            success: function () {
              setTimeout(function(){
                wx.navigateTo({
                  url: '/pages/company/companyCreatSuc/companyCreatSuc',
                })
              },1000)
            }
          })
        }
        else if (res.data.code == 100101) {
          // token无效，获取新的token
          var newToken = util.refreshTokenFun(app.globalData.tokenMap.refresh_token).then(
            function (res) {
              if (res.data.code == 200) {
                that.saveBtnRegFun();
              }
              else if (res.data.code == 400) {
                var wxLoginFun = util.wxLoginFun().then(
                  function (res2) {
                    if (res2.data.code == 200) {
                      that.saveBtnRegFun();
                    }
                  }
                )
              }
            }
          ).catch(
            function (err) {
              console.log("获取新的token失败返回", err)
            }
          )
        }
      },
      fail: function (err) {
        console.log("底部确认fail返回", err)
      },
      complete:function(){
        that.setData({
          loadingType: false
        })
      }
    })
  }

  
})